import { Controller, Post, Get, Body, Param, UseGuards, Query,Render } from '@nestjs/common';
import { PaymentService } from './payment.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { CurrentUser } from '../auth/current-user.decorator';
import { User } from '../entity/user.entity';
import { PaymentMethod } from '../entity/payment.entity';

@Controller('payments')
export class PaymentController {
  constructor(protected readonly paymentService: PaymentService) {}
  @UseGuards(JwtAuthGuard)
  @Post('orders/:orderId')
  createPayment(
    @Param('orderId') orderId: string,
    @CurrentUser() user: User,
    @Body() data: { method: PaymentMethod },
  ) {
      console.log('user:', user)
      console.log('orderId:', orderId)
      console.log('method:',data.method)
    return this.paymentService.createPayment(+orderId, user.id, data.method);
  }
  @UseGuards(JwtAuthGuard)
  @Get(':id')
  queryPayment(@Param('id') id: string) {
    return this.paymentService.queryPayment(+id);
  }
  @UseGuards(JwtAuthGuard)
  @Post(':id/refund')
  @UseGuards(RolesGuard)
//   @Roles('admin')
  refundPayment(@Param('id') id: string) {
      console.log('refoundId',id)
    return this.paymentService.refundPayment(+id);
  }
  // @UseGuards(JwtAuthGuard)
  @Get('notify/alipay')
  async alipayNotify(@Query() query: Record<string, any>) {
    console.log('query:',query)
      // TODO: 实现支付宝支付回调处
      return { success: true };

  }
@UseGuards(JwtAuthGuard)
  @Get('notify/wechat')
  async wechatNotify(@Query() query: Record<string, any>) {
    // TODO: 实现微信支付回调处理
    return { success: true };
  }


  @Render('alipay/index')
  @Get('alipay/index')
   // @UseGuards(JwtAuthGuard)
  alipayPay(): { result:Object} {

    const result = this.paymentService.getAliPayFrom()
    console.log('100000000000000-pay:',result)
    return {
            result
          }
      }
}

